<script setup>
import {gameDetailAddAPI} from "@/api/AdminApi/game.js";
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
import {ref} from "vue";
import {Plus} from "@element-plus/icons-vue";
import {ElMessage} from "element-plus";

const gameDetailInfo = ref({
  gameId: '',
  widerPicture: 'String',
  higherPicture: 'String',
  simplePicture: 'String',
  post: 'String',
  introduction: '',
  description: ''
})
const widerPicture = ref()
const higherPicture = ref()
const simplePicture = ref()
const post = ref()

// 判断图片
const judgeFile = (file) => {
  // 检查文件是否为 JPG 格式
  if (file.type !== 'image/jpeg') {
    ElMessage.error('文件格式必须是jpg!')
    return false;
  } else if (file.size / 1024 / 1024 > 10) {
    ElMessage.error('文件大小不得超过 10MB!')
    return false
  }
  return true
}

// 横向图片
const widerPictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  if (judgeFile(file)) {
    widerPicture.value = file
    gameDetailInfo.value.widerPicture = URL.createObjectURL(file);
  }
}

// 纵向图片
const higherPictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  if (judgeFile(file)) {
    higherPicture.value = file
    gameDetailInfo.value.higherPicture = URL.createObjectURL(file);
  }
}

// 精简图片
const simplePictureUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  if (judgeFile(file)) {
    simplePicture.value = file
    gameDetailInfo.value.simplePicture = URL.createObjectURL(file);
  }
}

// 海报
const postUpload = (uploadFile) => {
  const file = uploadFile.raw || uploadFile;
  if (judgeFile(file)) {
    post.value = file
    gameDetailInfo.value.post = URL.createObjectURL(file);
  }
}

// 定义工具栏选项
const toolbarOptions = {
  container: [
    ['bold', 'italic', 'underline'],  // 加粗、斜体、下划线按钮
    [{'header': 1}, {'header': 2}],  // 标题1、标题2
    ['image']  // 插入图片按钮
  ]
};

const rules = {
  introduction: [
    {required: true, message: '请输入介绍', trigger: 'blur'}
  ],
  description: [
    {required: true, message: '请输入描述', trigger: 'blur'}
  ],
  widerPicture: [
    {required: true, message: '请选择横向图片', trigger: 'blur'}
  ],
  higherPicture: [
    {required: true, message: '请选择纵向图片', trigger: 'blur'}
  ],
  simplePicture: [
    {required: true, message: '请选择精简图片', trigger: 'blur'}
  ],
  post: [
    {required: true, message: '请选择海报', trigger: 'blur'}
  ]
}

const formRef = ref()
// 修改按钮
const submit = async (gameId) => {
  gameDetailInfo.value.gameId = gameId
  const res = await gameDetailAddAPI(
      gameDetailInfo.value,
      widerPicture.value,
      higherPicture.value,
      simplePicture.value,
      post.value)
  if (res.code === 1) {
    return res.code
  } else {
    ElMessage.error(res.msg)
  }
}

const validate = async () => {
  if (gameDetailInfo.value.description === null || gameDetailInfo.value.description === '') {
    return false
  }
  if (widerPicture.value === null || higherPicture.value === null || simplePicture.value === null || post.value === null) {
    return false
  }
  try {
    const isValid = await formRef.value.validate();
    if (isValid) {
      return true
    }
  } catch (error) {
    return false
  }
}

// 父组件调用提交方法
defineExpose({
  submit: submit,
  validate: validate
})
</script>

<template>
  <el-form ref="formRef" :model="gameDetailInfo" :rules="rules" label-width="auto" style="width: 1280px;">
    <el-form-item label="横向图片" prop="widerPicture">
      <el-upload
          ref="uploadRef"
          :auto-upload="false"
          class="icon-uploader"
          :show-file-list="false"
          :on-change="widerPictureUpload">
        <el-image v-if="gameDetailInfo.widerPicture" :src="gameDetailInfo.widerPicture" class="widerPicture"/>
        <el-icon v-else class="icon-uploader-icon">
          <Plus/>
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="纵向图片" prop="higherPicture">
      <el-upload
          ref="uploadRef"
          :auto-upload="false"
          class="icon-uploader"
          :show-file-list="false"
          :on-change="higherPictureUpload">
        <el-image v-if="gameDetailInfo.higherPicture" :src="gameDetailInfo.higherPicture" class="higherPicture"/>
        <el-icon v-else class="icon-uploader-icon">
          <Plus/>
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="精简图片" prop="simplePicture">
      <el-upload
          ref="uploadRef"
          :auto-upload="false"
          class="icon-uploader"
          :show-file-list="false"
          :on-change="simplePictureUpload">
        <el-image v-if="gameDetailInfo.simplePicture" :src="gameDetailInfo.simplePicture" class="simplePicture"/>
        <el-icon v-else class="icon-uploader-icon">
          <Plus/>
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="海报" prop="post">
      <el-upload
          ref="uploadRef"
          :auto-upload="false"
          class="icon-uploader"
          :show-file-list="false"
          :on-change="postUpload">
        <el-image v-if="gameDetailInfo.post" :src="gameDetailInfo.post" class="post"/>
        <el-icon v-else class="icon-uploader-icon">
          <Plus/>
        </el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="介绍" prop="introduction">
      <el-input
          v-model="gameDetailInfo.introduction"
          type="textarea"
          :rows="10"
          style="resize: none;"
      />
    </el-form-item>
    <el-form-item label="描述" prop="description">
      <div class="editor">
        <quill-editor
            ref="editorRef"
            v-model:content="gameDetailInfo.description"
            content-type="html"
            theme="snow"
            :toolbar="toolbarOptions"
        >
        </quill-editor>
      </div>
    </el-form-item>
  </el-form>
</template>

<style lang="scss" scoped>
.icon-uploader {
  :deep {
    .widerPicture {
      width: 460px;
      height: 215px;
      display: block;
    }

    .higherPicture {
      width: 300px;
      height: 450px;
      display: block;
    }

    .simplePicture {
      width: 186px;
      height: 69px;
      display: block;
    }

    .post {
      width: 616px;
      height: 353px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.icon-uploader-icon {
      font-size: 40px;
      color: #8c939d;
      width: 278px;
      height: 278px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;
  height: 800px;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
